package com.example.service.impl;
import com.example.dao.OrderMapper;
import com.example.microinvestcommon.entity.UserOrder;
import com.example.microinvestcommon.pojo.Result;
import com.example.microinvestcommon.service.RedisService;
import com.example.microinvestcommon.vo.UserOrderVo;
import com.example.microinvestmarket.service.UserTradeImitateService;
import com.example.microinvestmq.producer.OrderProducer;
import com.example.service.CancelTradeService;
import com.example.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class OrderServiceImpl implements OrderService {

    private final OrderProducer orderProducer;
    private final RedisService redisService;
    private final OrderMapper orderMapper;
    private final CancelTradeService cancelTradeService;
    private final UserTradeImitateService userTradeImitateService;
    private static final Logger logger = LoggerFactory.getLogger(OrderProducer.class);

    @Autowired
    OrderServiceImpl(OrderProducer orderProducer,RedisService redisService,OrderMapper orderMapper,CancelTradeService cancelTradeService,UserTradeImitateService userTradeImitateService){
        this.orderProducer=orderProducer;
        this.redisService=redisService;
        this.orderMapper=orderMapper;
       this.cancelTradeService=cancelTradeService;
       this.userTradeImitateService=userTradeImitateService;
    }

    //挂单购买
    public Result<String> pushBuyOrderService(UserOrder userOrder,double nowPrice) {
        //模拟花费时间
        long ttl=userTradeImitateService.UserBuyTimeImitate(userOrder.getOrderAmount(), BigDecimal.valueOf(nowPrice));
        try {
            Result result=orderProducer.buyOrderProducer(userOrder,ttl);
            return result;
        }
        catch (Exception E) {
            return Result.error("发送失败"+E.getMessage());
        }

    }

    //挂单卖出
    @Override
    public Result<String> pushSellOrderService(UserOrder userOrder,double nowPrice) {
        //模拟花费时间
        long ttl=userTradeImitateService.UserSellTimeImitate(userOrder.getOrderAmount(),BigDecimal.valueOf(nowPrice));
        try {
            Result result=orderProducer.sellOrderProducer(userOrder,ttl);
            return result;
        }
        catch (Exception E) {
            return Result.error("发送失败"+E.getMessage());
        }
    }


    @Override
    @Transactional
    public Result<String> cancelOrder(Long orderId,Long userId) {
        // 不存在此订单
        if (!redisService.isOrderIdExist(String.valueOf(orderId),String.valueOf(userId))) {
            return Result.error("此订单已经完成");
        }

        // 存在则移除
        redisService.removeOrderId(String.valueOf(orderId),String.valueOf(userId));
        UserOrderVo userOrderVo = orderMapper.cancelUserOrder(orderId);

        // 更改订单状态
        orderMapper.orderStatusCancel(orderId);

        // 处理订单类型
        switch (userOrderVo.getBuySell()) {
            case "buy":
                cancelTradeService.transactBuyTreadService(userOrderVo);
                logger.info("订单已经撤销");
                return Result.success("订单已经撤销");
            case "sell":
               cancelTradeService.transactSellTreadService(userOrderVo);
                logger.info("订单已经撤销");
                return Result.success("订单已经撤销");
            default:
                return Result.error("无效的订单状态：" + userOrderVo.getBuySell());
        }
    }

}
